<?php
/*
 * Copyright 2008 by Oliver Radwan, Maxwell Palmer, Nolan McNair,
 * Taylor Talmage, and Allen Tucker.  This program is part of RMH Homebase.
 * RMH Homebase is free software.  It comes with absolutely no warranty.
 * You can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation
 * (see <http://www.gnu.org/licenses/ for more information).
*/

/**
 * @version May 1, 2008
 * @author Oliver Radwan and Allen Tucker
 */

include_once('dbinfo.php');
include_once('Person.php');

function setup_dbPersons() {
		connect();
		mysql_query("DROP TABLE IF EXISTS dbPersons");
		$result = mysql_query("CREATE TABLE dbPersons(id TEXT NOT NULL, first_name TEXT NOT NULL, last_name TEXT, " .
				"    address TEXT, city TEXT, state VARCHAR(2), zip TEXT, phone1 VARCHAR(12) NOT NULL, phone2 VARCHAR(12), " .
    			"    email TEXT, type TEXT, status TEXT, occupation TEXT, refs TEXT, maywecontact TEXT," .
				"    motivation TEXT, specialties TEXT, " .
				"    availability TEXT, schedule TEXT, history TEXT, " .
				"    birthday TEXT, start_date TEXT, notes TEXT, password TEXT)");
		if (!result)
		   echo mysql_error() . "Error creating dbPersons table<br>";
		$admin_acct = new Person("admin","admin","", "", "", "","","", "", "manager","active","", "", "", "","",
			"", "", "", "","",
			"This is the default admin account.  It should not be used for anything but initial editing, and then it should be deleted.",
			"admin");
	    $result=add_person($admin_acct);
        if(!$result)
            echo mysql_error()."Error: admin account not created.";
        else echo("<br />Admin account created.  username = admin, password = admin.<br />");
	$p = new Person("Allen", "Tucker","42 Walini Way","Harpswell","ME","04079","2078419604","","allen@bowdoin.edu","manager", "active","", "", "", "","",
    "Mon9-12","","","02-19-89", "03-14-08","","Allen2078419604");
    add_person($p);
	$p = new Person("Sara", "Funaiock","Ronald McDonald House","Wilmington","DE","19803","3024285340","","s.funaiock@rmhde.org","manager", "active","", "", "", "", "",
    "Mon9-12","","","02-19-89", "03-14-08","","Sara3024285340");
    add_person($p);
    
}
/*
 * add a person to dbPersons table: if already there, return false
 */
	function add_person($person){
		if(! $person instanceof Person) die("Error: add_person type mismatch");
		connect();
		$query = "SELECT * FROM dbPersons WHERE id = '".$person->get_id()."'";
		$result = mysql_query($query);
		//if there's no entry for this id, add it
		if ($result==null || mysql_num_rows($result) == 0) {
   			mysql_query('INSERT INTO dbPersons VALUES("'.
		             $person->get_id().'","'.
		             $person->get_first_name().'","'.
		             $person->get_last_name().'","'.
		             $person->get_address().'","'.
		             $person->get_city().'","'.
		             $person->get_state().'","'.
		             $person->get_zip().'","'.
		             $person->get_phone1().'","'.
		             $person->get_phone2().'","'.
		             $person->get_email().'","'.
		             implode(',', $person->get_type()).'","'.
		             $person->get_status().'","'.
		             $person->get_occupation().'","'.
		             $person->get_references().'","'.
		             $person->get_maywecontact().'","'.
		             $person->get_motivation().'","'.
		             $person->get_specialties().'","'.
		             implode(',', $person->get_availability()).'","'.
		             implode(',', $person->get_schedule()).'","'.
		             implode(',', $person->get_history()).'","'.
		             $person->get_birthday().'","'.
		             $person->get_start_date().'","'.
                     $person->get_notes().'","'.
                     $person->get_password().
                     '");');
            mysql_close();
            return true;
   		}
   		mysql_close();
   		return false;
   	}
/*
 * remove a person from dbPersons table.  If already there, return false
 */
	function remove_person ($id) {
		connect();
   		$query = 'SELECT * FROM dbPersons WHERE id = "'.$id.'"';
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) == 0) {
		   mysql_close();
		   return false;
		}
		$query='DELETE FROM dbPersons WHERE id = "'.$id.'"';
		$result=mysql_query($query);
		mysql_close();
		return true;
	}
/*
 * @return a single row from dbPersons table matching a particular id.
 * if not in table, return false
 */
	function get_person($id){
		connect();
   		$query = 'SELECT * FROM dbPersons WHERE id = "'.$id.'"';
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) !== 1) {
		   mysql_close();
		   return false;
		}
		mysql_close();
   		return $result;
	}

	function change_password($id,$newPass){
		connect();
		$query = 'UPDATE dbPersons SET password = "'.$newPass.'" WHERE id = "'.$id.'"';
		$result = mysql_query($query);
		mysql_close();
		return $result;
	}
	
	function resetall_passwords(){
		$result = getall_persons();
		echo mysql_num_rows($result) . "<br>";
	    while($thisRow = mysql_fetch_array($result, MYSQL_ASSOC)) {
	        $id = $thisRow['id']; 
	        $newPass = md5($id);
	        echo $id . $newPass . "<br>";
            change_password($id, $newPass);
		}
		return "All Passwords Reset";
	}
/*
 * @return all rows from dbPersons table ordered by last name
 * if none there, return false
 */

	function getall_persons(){
		connect();
   		$query = "SELECT * FROM dbPersons ORDER BY last_name,first_name";
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) == 0) {
		   mysql_close();
		   return false;
		}
		mysql_close();
   		return $result;
	}

	function getall_names($status) {
		connect();
		$result = mysql_query("SELECT id,first_name,last_name,type FROM dbPersons " .
				"WHERE status = '".$status."'");
		mysql_close();
   		return $result;
	}
/*
 * @return all people of type $t from dbPersons table ordered by last name
 */
	function getall_type($t){
		connect();
   		$query = "SELECT * FROM dbPersons WHERE type LIKE '%" . $t . "%' ORDER BY last_name,first_name";
		$result = mysql_query($query);
		if ($result==null || mysql_num_rows($result) == 0) {
		   mysql_close();
		   return false;
		}
		mysql_close;
   		return $result;
	}
/*
 *   get all volunteers available for venue $venue on a particular $day and $time
 */
function getall_available($venue, $day, $time) {
	connect();
    $query="SELECT * FROM dbPersons WHERE (type LIKE '%" . $venue . "%')" .
			" AND availability LIKE '%" . $day . $time .
			"%' ORDER BY last_name,first_name";
	$result=mysql_query($query);
	mysql_close();
	return $result;
}

?>
